# Encoding: UTF-8


calculate_statistic <- function(data = qualtrics_df_ft, statistic, by = NULL) {
  calculations <- cj(data = data,
                     formula = select ~ `Acceptance Rate` + Audience +
                       `Editorial Location` + Language + Methods +
                       `Peer Pressure` + `Response Time` + Scimago,
                     id = ~ ResponseId,
                     estimate = statistic,
                     by = by)
  return(calculations)
}

calculate_statistics <- function(data = qualtrics_df_ft, statistics,
                                 by = NULL) {
  calculations_all_statistics <- data.frame()
  for (statistic in statistics) {
    statistic <- noquote(statistic)
    calculations_one_statistic <- calculate_statistic(data = data,
                                                      statistic = statistic,
                                                      by = by)
    calculations_one_statistic$statistic <- statistic
    calculations_all_statistics <- rbind(calculations_all_statistics,
                                         calculations_one_statistic)
  }
  calculations_all_statistics <- calculations_all_statistics %>%
    rename(Feature = "feature")
  return(calculations_all_statistics)
}

plot_no_subset <- function(data = qualtrics_df_ft, width = 7, height = 5) {
  
  calculations_all_statistics <- calculate_statistics(data = data,
                                                      statistics = c("amce", "mm"))
  
  calculations_all_statistics$statistic <- factor(calculations_all_statistics$statistic)
  levels(calculations_all_statistics$statistic) <- list(AMCE = "amce", 
                                                        `Marginal Means` = "mm")
  
  vline_data <- data.frame(statistic = unique(calculations_all_statistics$statistic),
                           xintercept = c(0, 0.5))
  
levels(calculations_all_statistics$Feature)[levels(calculations_all_statistics$Feature)=="Peer Pressure"]<-"Colleagues Published"
levels(calculations_all_statistics$Feature)[levels(calculations_all_statistics$Feature)=="Methods"]<-"Relevance of Methods"

  plot <- ggplot(data = calculations_all_statistics,
                 mapping = aes(x = upper, y = rev(level))) +
    geom_vline(data = vline_data, aes(xintercept = xintercept), size = 0.3) +
    geom_errorbar(mapping = aes(xmin = lower, xmax = upper),
                  width = 0.7, size = 0.5) +
    geom_point(mapping = aes(x = estimate), size = 1) +
    scale_y_discrete(limits = rev) +
    facet_wrap(facets = ~ statistic, scales = "free_x") +
    scale_y_discrete(labels = rev(interaction(calculations_all_statistics$Feature,
                                              calculations_all_statistics$level,
                                              sep = ": "))) +
    theme_bw() +
    theme(axis.title = element_blank(), legend.position = "none")
  ggsave(filename = paste(path_plots, data_date, "01_general", type, sep = ""),
         plot = plot, width = width, height = height, units = "cm")
}

plot_subset <- function(data = qualtrics_df_ft, by, by_name, out = c(), title,
                        filename, width = 14, height = 7) {
  
  calculations_amce_mm <- calculate_statistics(data = data,
                                               statistics = c("amce", "mm"),
                                               by = by)
  calculations_mm.diff <- calculate_statistics(data = data,
                                               statistics = c("mm_differences"),
                                               by = by)
  calculations_all_statistics <- rbind(calculations_amce_mm,
                                       calculations_mm.diff)
  
  calculations_all_statistics$statistic <- factor(calculations_all_statistics$statistic)
  levels(calculations_all_statistics$statistic) <- list(`AMCE`="amce",
                                                        `Marginal Means` = "mm",
                                                        `Differences in Marginal Means` = "mm_differences"
                                                       )
  
  vline_data <- data.frame(statistic = unique(calculations_all_statistics$statistic),
                           xintercept = c(0, 0.5, 0))
  
  selected<- setdiff(unique(calculations_all_statistics$Feature), out)
  calculations_all_statistics<-calculations_all_statistics%>%
    filter(Feature %in% selected)
  
  levels(calculations_all_statistics$Feature)[levels(calculations_all_statistics$Feature)=="Peer Pressure"]<-"Colleagues Published"
  levels(calculations_all_statistics$Feature)[levels(calculations_all_statistics$Feature)=="Methods"]<-"Relevance of Methods"
  
  plot <- ggplot(data = calculations_all_statistics,
                 mapping = aes(x = upper, y = rev(level), color = BY)) +
    geom_vline(data = vline_data, aes(xintercept = xintercept), size = 0.3) +
    geom_errorbar(mapping = aes(xmin = lower, xmax = upper),
                  width = 0.7, size = 0.5,
                  position = position_dodge(width = 1)) +
    geom_point(mapping = aes(x = estimate), size = 1,
               position = position_dodge(width = 1)) +
    scale_y_discrete(labels = rev(interaction(calculations_all_statistics$Feature,
                                              calculations_all_statistics$level,
                                              sep = ": "))) +
    scale_color_hue(name = by_name, direction = -1, c = 150) +
    facet_wrap(facets = ~ statistic, scales = "free_x") +
    labs(title = title) +
    theme_bw() +
    theme(axis.title = element_blank(), legend.position = "bottom", 
          axis.text.y = element_text(size=13), axis.text.x = element_text(size=13),
          legend.text = element_text(size=13), title = element_text(size=15))
  ggsave(filename = paste(path_plots, data_date, filename, type, sep = ""),
         plot = plot, width = width, height = height, units = "in")
}


plot_subset_nosave <- function(data = qualtrics_df_ft, by, by_name, out = c(), 
                        filename, width = 14, height = 7) {
  
  calculations_amce_mm <- calculate_statistics(data = data,
                                               statistics = c("amce", "mm"),
                                               by = by)
  calculations_mm.diff <- calculate_statistics(data = data,
                                               statistics = c("mm_differences"),
                                               by = by)
  calculations_all_statistics <- rbind(calculations_amce_mm,
                                       calculations_mm.diff)
  
  calculations_all_statistics$statistic <- factor(calculations_all_statistics$statistic)
  levels(calculations_all_statistics$statistic) <- list(`AMCE`="amce",
                                                        `Marginal Means` = "mm",
                                                        `Differences in Marginal Means` = "mm_differences"
  )
  
  vline_data <- data.frame(statistic = unique(calculations_all_statistics$statistic),
                           xintercept = c(0, 0.5, 0))
  
  selected<- setdiff(unique(calculations_all_statistics$Feature), out)
  calculations_all_statistics<-calculations_all_statistics%>%
    filter(Feature %in% selected) 
  
  levels(calculations_all_statistics$Feature)[levels(calculations_all_statistics$Feature)=="Peer Pressure"]<-"Colleagues Published"
  levels(calculations_all_statistics$Feature)[levels(calculations_all_statistics$Feature)=="Methods"]<-"Relevance of Methods"
  
  
  
 plot<-ggplot(data = calculations_all_statistics,
                 mapping = aes(x = upper, y = rev(level), color=BY, shape=BY, linetype=BY)) +
    geom_vline(data = vline_data, aes(xintercept = xintercept), size = 0.3) +
    geom_errorbar(mapping = aes(xmin = lower, xmax = upper),
                  width = 0.75, size = 0.4,
                  position = position_dodge(width = 1)) +
    geom_point(mapping = aes(x = estimate ), size = 1,
               position = position_dodge(width = 1)) +
    scale_y_discrete(labels=rev(calculations_all_statistics$level)) +
    facet_wrap(facets = ~ statistic, scales = "free_x") +
    labs(title = calculations_all_statistics$Feature ) +
    theme_bw() +
    theme(axis.title = element_blank(), legend.position = "bottom", 
          axis.text.y = element_text(size=10), axis.text.x = element_text(size=10),
          legend.text = element_text(size=10), title = element_text(size=13))+
  scale_linetype_manual(values=c(1,1,6))+
  scale_color_manual(values=c("black", "darkgrey", "black"))+
  labs(color=by_name, shape=by_name, linetype=by_name)
  }


plot_subset_attribute <- function(data = qualtrics_df_ft, by, by_name,  attribute, title,
                        filename, width = 14, height = 7) {
  
  calculations_amce_mm <- calculate_statistics(data = data,
                                               statistics = c("mm"),
                                               by = by)
  calculations_all_statistics <- calculations_amce_mm
  vline_data <- data.frame(statistic = unique(calculations_all_statistics$statistic),
                           xintercept = c(0.5))
  calculations_all_statistics<-calculations_all_statistics%>%
    filter(Feature %in% attribute)
  
  levels(calculations_all_statistics$Feature)[levels(calculations_all_statistics$Feature)=="Peer Pressure"]<-"Colleagues Published"
  levels(calculations_all_statistics$Feature)[levels(calculations_all_statistics$Feature)=="Methods"]<-"Relevance of Methods"
  
  plot<-ggplot(data = calculations_all_statistics,
                 mapping = aes(x = upper, y = rev(BY))) +
    geom_vline(data = vline_data, aes(xintercept = xintercept), size = 0.3) +
    geom_errorbar(mapping = aes(xmin = lower, xmax = upper),
                  width = 0.4, size = 0.5,
                  position = position_dodge(width = 1))+
    geom_point(mapping = aes(x = estimate), size = 1,
               position = position_dodge(width = 1)) +
    scale_y_discrete(labels = rev(unique(calculations_all_statistics$BY))) +
    
      scale_color_hue(name = by_name, direction = -1, c = 150) +
    facet_wrap(facets = ~ level, scales = "free_x") +
    labs(title = title
    ) +
    theme_bw() +
    theme(axis.title = element_blank(), legend.position = "none", 
          axis.text.y = element_text(size=10), axis.text.x = element_text(size=10),
          legend.text = element_text(size=10), title = element_text(size=13))
  ggsave(filename = paste(path_plots, data_date, filename, type, sep = ""),
         plot = plot, width = width, height = height, units = "in")
}